//
//  InstitutionsDetailContainViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/11.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "InstitutionsDetailContainViewController.h"
#import "FlipTableView.h"
#import "SegmentTapView.h"
#import "InstituDetailViewController.h"
#import "InstituIntroduceViewController.h"
#import "InstituteModel.h"
#import "AddressNavgationViewController.h"
#import "AddressInstituteNearbyViewController.h"
@interface InstitutionsDetailContainViewController ()<SegmentTapViewDelegate,FlipTableViewDelegate,UIViewControllerTransitioningDelegate>

@property (nonatomic, strong)SegmentTapView *segment;
@property (nonatomic, strong)FlipTableView *flipView;
@property (strong, nonatomic) NSMutableArray *controllsArray;
@property(nonatomic,strong)UIImageView *picImageView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UIImageView *logoImageView;
@property(nonatomic,strong)UILabel *titleLabel;

@end

@implementation InstitutionsDetailContainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self initSegment];
//    [self initFlipTableView];
    [self getDataInformation];
    // Do any additional setup after loading the view.
}

/**
 *  分段选择的标题设置
 *
 */
-(void)initSegment:(NSString *)imagepath withLogoPath:(NSString *)logoPath WithTitle:(NSString *)title{
    self.title = title;
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, scaleTopAndBottom(200))];
    [self.view addSubview:self.picImageView];
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:imagepath] placeholderImage:[UIImage imageNamed:@""]];
    self.logoImageView = [[UIImageView alloc] init];
    [self.picImageView addSubview:self.logoImageView];
    self.logoImageView.layer.cornerRadius = 40;
    self.logoImageView.layer.masksToBounds = YES;
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.picImageView.mas_centerY).offset(scaleTopAndBottom(0));
        make.centerX.equalTo(self.picImageView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:logoPath] placeholderImage:[UIImage imageNamed:@""]];
    self.titleLabel = [[UILabel alloc] init];
    [self.picImageView addSubview:self.titleLabel];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.text = title;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.picImageView.mas_centerY).offset(scaleTopAndBottom(50));
        make.centerX.equalTo(self.picImageView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(WIDTH - 40, 20));
    }];
    self.segment = [[SegmentTapView alloc] initWithFrame:CGRectMake(0,64 + scaleTopAndBottom(200), WIDTH, 40) withDataArray:[NSArray arrayWithObjects:@"介绍",@"详情", nil] withFont:20];
    self.segment.delegate = self;
    self.segment.lineColor = COLOR_HEX(0xf9bc3d);
    self.segment.titleFont = scaleLeftAndRight(15);
    self.segment.textNomalColor = [UIColor lightGrayColor];
    self.segment.textSelectedColor = COLOR_HEX(0xf9bc3d);
    [self.view addSubview:self.segment];
    [self initFlipTableView];
}
-(void)initFlipTableView{
    InstituteModel *model = self.dataArray[0];
    InstituIntroduceViewController *instruVc = [[InstituIntroduceViewController alloc] init];
    instruVc.valueBlock = ^(NSString *title, NSString *address) {
        AddressNavgationViewController *navVc = [[AddressNavgationViewController alloc] init];
        
        navVc.nameStr = address;
        navVc.addressStr = title;
        NSLog(@"地址的%@",navVc.nameStr);
        NSLog(@"名字%@",navVc.addressStr);
        [self.navigationController pushViewController:navVc animated:YES];
    };
    instruVc.modelBlock = ^{
        AddressInstituteNearbyViewController *nearbyVc = [[AddressInstituteNearbyViewController alloc] init];
        nearbyVc.transitioningDelegate = self;
        nearbyVc.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:nearbyVc animated:YES completion:^{
                    
                }];
    };
    instruVc.instituId = self.instituId;
    instruVc.dataArray = self.dataArray;
    InstituDetailViewController *cataVc = [[InstituDetailViewController alloc] init];
    cataVc.dataArray = model.institutionImgs;
    [self.controllsArray addObject:instruVc];
    [self.controllsArray addObject:cataVc];
    self.flipView = [[FlipTableView alloc] initWithFrame:CGRectMake(0,64 + 40 + scaleTopAndBottom(200), WIDTH, HEIGHT - (64 + 40 + scaleTopAndBottom(200))) withArray:_controllsArray];
    self.flipView.delegate = self;
    [self.view addSubview:self.flipView];
    
}

#pragma mark -------- select Index
-(void)selectedIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
    [self.flipView selectIndex:index];
    
}
-(void)scrollChangeToIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
    [self.segment selectIndex:index];
}
-(NSMutableArray *)controllsArray{
    if (_controllsArray == nil) {
        _controllsArray = [[NSMutableArray alloc] init];
    }
    return _controllsArray;
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
/**
 获取机构数据
 */
-(void)getDataInformation{
    dispatch_queue_t gloabaQue = dispatch_get_global_queue(0, 0);
    dispatch_async(gloabaQue, ^{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSString *url = ServerIp@"/institutionApp/institutionDetailAll";
        NSDictionary *param = @{@"id":self.instituId};
         NSLog(@"%@",param);
        [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *instituDic = jsonDic[@"institution"];
            InstituteModel *model = [InstituteModel modelWithDictionary:instituDic];
            [self.dataArray addObject:model];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self initSegment:model.imgPath withLogoPath:model.logoPath WithTitle:model.name];
            });
            NSLog(@"哈哈数据%@",jsonDic);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];

    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
